class Login {
    //构造函数
    constructor() {
        //定义一个空变量作为回跳页面的地址
        this.returnUrl = '';
        //给登录按钮绑定点击事件
        this.$('.over').onclick = this.checkOver.bind(this)
        // console.log(this);
    }
    //检验登录
    checkOver() {
        //console.log(this);
        //获取表单元素
        // let form = document.forms.login.elements
        let form = this.$('form')
        let username = form.uname.value.trim()
        let password = form.password.value.trim()
        // console.log(username, password);
        //非空验证
        if (!username || !password) {
            //return alert('不能为空')
            layer.tips('账号或者密码不能为空', '.over', {
                tips: [1, '#3595CC'],
                time: 4000
            });
        }
        //定义一个参数  发送请求的时候用
        let param = `username=${username}&password=${password}`
        // console.log(param);
        //设置编码格式
        axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
        axios.post('http://localhost:8888/users/login', param).then(response => {
            console.log(response);
            let { data, status } = response
            //console.log(data, data.user);
            //获取注册过的用户的账号
            //
            // let rhy = data.user.username
            // if (username != rhy) return layer.tips('账号错误', '#uname');
            // console.log(rhy);
            //判断是否登录成功
            if (status != 200 || data.code != 1) {
                return layer.tips('账号或密码不正确', '#uname');


            }
            //存入token
            localStorage.setItem('token', data.token);
            //存入用户的ID
            localStorage.setItem('user_Id', data.user.id);
            //登录成功后回跳到list页面
            // this.returnUrl ? location.assign(this.returnUrl) : location.assign('./list.html');
            this.returnUrl ? location.assign(this.returnUrl) : location.assign('./list.html');
        })

    }

    //获取节点
    $(ele) {
        let res = document.querySelectorAll(ele)
        return res.length == 1 ? res[0] : res;
    }
}
new Login()